package com.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.util.HbnUtil;

import java.sql.*;

import entities.Customer;
import entities.Orders;
import entities.Product;

/**
 * Servlet implementation class CompleteOrder
 */
@WebServlet("/CompleteOrder")
public class CompleteOrder extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CompleteOrder() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		/*<%@ page import="oracle.jdbc.pool.OracleDataSource"%>
		<!-- Database lookup -->
		<%
		Connection conn = null;
		ResultSet rset = null;
		String error_msg = "";
		try {
		OracleDataSource ods = new OracleDataSource();
		ods.setURL("jdbc:oracle:thin:scott/tiger@//w4111b.cs.columbia.edu:1521/ADB");
		conn = ods.getConnection();
		Statement stmt = conn.createStatement();
		rset = stmt.executeQuery("select empno, ename, sal from emp");*/
		Orders order = new Orders();
		Session se = HbnUtil.getSession();
		
		HttpSession session = request.getSession();
		Customer customer = new Customer();
		Transaction tran = se.beginTransaction();
		String userid = session.getAttribute("Userid").toString();
		customer = (Customer)se.createQuery("from Customer where userid ='" + userid +"'").uniqueResult();
	    Product product = (Product)session.getAttribute("Product");
		order.setCustomer(customer);
		order.setProductid(product.getProductid());
		order.setQuantity(1);
		
		se.save(order);
		tran.commit();
		se.close();
		response.sendRedirect("showorder.jsp");
		
	}

}
